import Vue from 'vue'
// 路由整合
import VueRouter from 'vue-router'
import store from '@/store/user/index'
Vue.use(VueRouter)
// 引入路由
// 解决重复跳转问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
// 发现音乐页
import HomePage from '@/pages/Home/HomePage.vue'
import PersonRecom from '@/pages/Home/HomePageView/PersonRecom.vue'
import SongList from '@/pages/Home/HomePageView/SongList.vue'
import RankList from '@/pages/Home/HomePageView/RankList.vue'
import Singer from '@/pages/Home/HomePageView/Singer.vue'
import NewMusic from '@/pages/Home/HomePageView/NewMusic.vue'
// 视频页
import Mv from "@/pages/Mv"
// 搜索页
import Search from "@/pages/Seaech"
//精品歌单路由
import Highquality from "@/pages/playlist/Highquality.vue"
// 推荐歌单详情
import HomeItem from "@/pages/HomeItem/Home"
// 个人中心
import Personal from '@/pages/User'
// 每日推荐路由
import DailyRecom from '@/components/DailyRecom'
// 私人FM
import FM from "@/pages/Mv/FM.vue"
//mv详情
import Mvdetails from "@/pages/Mv/Mvdetails"
//歌手详情
import singerDetail from '@/components/Singer/singerDetail'
// }
// 配置路由
const router = new VueRouter({
  mode: 'hash',
  // 配置路由
  routes: [
    // 重定向默认访问首页
    {
      path: '/',
      component: HomePage,
      redirect: '/personrecom',
      // 发现音乐页
      children: [
        { path: "personrecom", component: PersonRecom, meta: { title: '个性推荐' } },
        { path: "songlist", component: SongList, meta: { title: '歌单' } },
        // 精品歌单路由
        { path: 'highquality/:cat', props: true, component: Highquality, meta: { title: '个性推荐' } },
        { path: "ranklist", component: RankList, meta: { title: '排行榜' } },
        { path: "singer", component: Singer, meta: { title: '歌手' } },
        { path: "newmusic", component: NewMusic, meta: { title: '最新音乐' } },
      ],
    },
    // 搜索路由
    { path: "/search", component: Search, name: "search", meta: { title: '搜索' } },
    // mv路由
    { path: "/mv", component: Mv, meta: { title: 'Mv' } },
    // 推荐歌单详情
    { path: '/homeitem/:id', component: HomeItem, name: 'home', meta: { title: '推荐歌单' } },
    // 专辑详情
    { path: '/albumdetail', component: () => import('@/pages/Albums/AlbumDetail.vue'), meta: { title: '专辑' } },
    // 个人中心
    { path: '/personal', component: Personal, meta: { title: '个人中心' } },
    // 每日推荐
    { path: '/dailyrecom', component: DailyRecom, meta: { isAuth: true, title: '每日推荐' } },
    // 私人FM
    { path: '/fm', component: FM, meta: { isAuth: false, title: '私人FM' } },
    // mv详情
    { path: '/mvdetails', component: Mvdetails, meta: { title: 'Mv' } },
    // 歌手详情界面
    { path: '/singerdetail', component: singerDetail, meta: { title: '歌手详情' } }
    // 
  ]
})
// 全局前置路由守卫---初始化的时候被调用，每次路由切换前被调用
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  if (to.meta.isAuth) {
    if (store.state.isLogin === true) {
      next()
    } else {
      Vue.prototype.$message.error('需要登录')
    }
  } else {
    next()
  }
})
// 全局后置路由守卫---初始化的时候被调用，每次路由切换前被调用
router.afterEach((to) => {
  document.title = to.meta.title//改变路由title
})
export default router